<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
        >搜索
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading1"
          @click="handleExport(true)"
        >导出呆滞物料
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading2"
          @click="handleExport(false)"
        >导出预警物料
        </el-button>
      </el-col>
      <!-- 分页效果 -->
      <el-col :span="2.2" style="position: absolute;right: 0px;top:0">
        <Page style="margin-right: 0px "
              :current="queryParams1.pageNum"
              :page-size="queryParams1.pageSize"
              :total="total1"
              @on-change="pageChange1"
              simple
              v-show="activeName==='first'"
        ></Page>
        <Page style="margin-right: 0px "
              :current="queryParams2.pageNum"
              :page-size="queryParams2.pageSize"
              :total="total2"
              @on-change="pageChange2"
              simple
              v-show="activeName==='second'"
        ></Page>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 查询对话框   -->
    <el-dialog :title="title"
               v-model="openSearch"
               :close-on-click-modal="false"
               draggable width="400px"
               append-to-body
    >
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-col :span="24">
          <el-form-item label="月份">
            <el-date-picker
              v-model="date"
              :type="'month'"
              :format="'yyyy-MM'"
              autocomplete="off"
              :clearable="false"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="物料代码" prop="materialCode">
            <el-input
              v-model="queryParams.materialCode"
              placeholder="请输入物料代码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="客户编码" prop="customerCode">
            <el-input
              v-model="queryParams.customerCode"
              placeholder="请输入客户编码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item style="margin-top: 25px; display: flex;justify-content: flex-end">
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

      <el-tabs v-model="activeName">
        <el-tab-pane label="呆滞物料" name="first">
          <el-table
            highlight-current-row
            border
            stripe
            id="deadMaterial"
            v-loading="loading1"
            :data="sectionDeadMaterialsList"
            height="640px"
            :header-cell-style="{'text-align':'center'}"
          >
            <el-table-column label="#" fixed="left" type="index" width="40" align="center"/>
            <el-table-column label="物料代码" :show-overflow-tooltip="true" width="120" align="center" prop="kcdWldm"/>
            <el-table-column label="客户编码" :show-overflow-tooltip="true" width="150" align="center" prop="itmCustwldm"/>
            <el-table-column label="品名规格" :show-overflow-tooltip="true" align="center" prop="itmPmgg"/>
            <el-table-column label="出库数量" :show-overflow-tooltip="true" width="120" align="center" prop="kcdZjkc"/>
            <el-table-column label="期初库存" :show-overflow-tooltip="true" width="120" align="center" prop="kcdQqkc"/>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="预警物料" name="second">
          <el-table
            highlight-current-row
            border
            stripe
            id="warningMaterial"
            v-loading="loading2"
            :data="sectionWarningMaterialsList"
            height="640px"
            :header-cell-style="{'text-align':'center'}"
          >
            <el-table-column label="#" fixed="left" type="index" width="40" align="center"/>
            <el-table-column label="物料代码" :show-overflow-tooltip="true" width="120" align="center" prop="kcdWldm"/>
            <el-table-column label="客户编码" :show-overflow-tooltip="true" width="150" align="center" prop="itmCustwldm"/>
            <el-table-column label="品名规格" :show-overflow-tooltip="true" align="center" prop="itmPmgg"/>
            <el-table-column label="出库数量" :show-overflow-tooltip="true" width="120" align="center" prop="kcdZjkc"/>
            <el-table-column label="期初库存" :show-overflow-tooltip="true" width="120" align="center" prop="kcdQqkc"/>
          </el-table>
        </el-tab-pane>
      </el-tabs>

  </div>
</template>

<script>
import {
  exportSectionDeadMaterials,
  listSectionDeadMaterials,
} from "@/api/repertory/kcmData";

export default {
  name: "SectionDeadMaterialsReport",
  data() {
    return {
      // 遮罩层
      loading1: true,
      loading2: true,
      // 导出遮罩层
      exportLoading1: false,
      exportLoading2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: document.body.clientHeight > 900 ? 25 : 20,
        materialCode: null,
        customerCode: null,
        year: this.$moment(this.date).format('yyyy'),
        month: this.$moment(this.date).format('MM'),
        flag: null,
      },
      // 查询参数
      queryParams1: {
        pageNum: 1,
        pageSize: document.body.clientHeight > 900 ? 25 : 20
      },
      // 查询参数
      queryParams2: {
        pageNum: 1,
        pageSize: document.body.clientHeight > 900 ? 25 : 20
      },
      // 弹出层标题
      title: '',
      // 显示搜索条件
      showSearch: true,
      // 是否显示弹出层
      openSearch: false,
      // 总条数
      total1: 0,
      total2: 0,
      date: this.$moment().subtract(1, "months"),
      // 呆滞物料
      sectionDeadMaterialsList: [],
      // 预警物料
      sectionWarningMaterialsList: [],
      activeName: 'first',
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading1 = true
      this.loading2 = true
      this.queryParams.year = this.$moment(this.date).format('yyyy')
      this.queryParams.month = this.$moment(this.date).format('MM')
      this.queryParams.flag = '0'
      listSectionDeadMaterials(this.queryParams)
        .then(res => {
          this.sectionDeadMaterialsList = res.rows
          this.total1 = res.total
          this.loading1 = false
        })
        .catch(error => {
          console.log(error)
          this.loading1 = false
        })
      this.queryParams.flag = '1'
      listSectionDeadMaterials(this.queryParams)
        .then(res => {
          this.sectionWarningMaterialsList = res.rows
          this.total2 = res.total
          this.loading2 = false
        })
        .catch(error => {
          console.log(error)
          this.loading2 = false
        })
    },
    reset() {
      this.queryParams.materialCode = null
      this.queryParams.customerCode = null
    },
    cancel() {
      this.openSearch = false
      this.reset()
    },
    handleSearch() {
      this.title = '查询呆滞物料'
      this.openSearch = true
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.queryParams1.pageNum = 1
      this.queryParams2.pageNum = 1
      this.getList()
      this.openSearch = false
    },
    /** 导出数据 */
    handleExport(flag) {
      const queryParams = this.queryParams
      this.$confirm('确认要导出所有' + (flag ? '呆滞' : '预警') + '物料数据吗？', '确认', {
        cancelButtonText: '取消',
        confirmButtonText: '确认',
      })
        .then(() => {
          flag ? this.exportLoading1 = true : this.exportLoading2 = true
          this.queryParams.flag = flag ? '0' : '1'
          return exportSectionDeadMaterials(queryParams)
        })
        .then(res => {
          this.download(res.msg)
          flag ? this.exportLoading1 = false : this.exportLoading2 = false
        })
        .catch(error => {
          console.log(error)
          flag ? this.exportLoading1 = false : this.exportLoading2 = false
        })
    },
    /** 分页 */
    pageChange1(page) {
      this.queryParams1.pageNum = page
      this.queryParams.pageNum = page
      this.queryParams.flag = '0'
      listSectionDeadMaterials(this.queryParams)
        .then(res => {
          this.sectionDeadMaterialsList = res.rows
          this.loading1 = false
        })
        .catch(error => {
          console.log(error)
          this.loading1 = false
        })
    },
    pageChange2(page) {
      this.queryParams2.pageNum = page
      this.queryParams.pageNum = page
      this.queryParams.flag = '1'
      listSectionDeadMaterials(this.queryParams)
        .then(res => {
          this.sectionWarningMaterialsList = res.rows
          this.loading2 = false
        })
        .catch(error => {
          console.log(error)
          this.loading2 = false
        })
    },
  },
}
</script>

<style lang="scss" scoped>

</style>
